<?php

class Ms_Model_ForumPost extends Zend_Db_Table_Abstract
{

    protected $_name = 'forum_posts';

    protected $_referenceMap = array(
        'Post' => array(
            'columns'  =>  array('topic_id'),
            'refTableClass'  => 'Ms_Model_ForumTopic',
            'refColumns'     => array('id'),
            'onDelete'       => self::CASCADE
        )
    );
    
    /**
     * 获取某一帖子
     * 
     * @param int $topicId
     */
    public function getPost($topicId){
        $row = $this->find($topicId)->current();
        
        return $row;
    }
    
    /**
     * 获取回复帖列表
     * 
     * @param int $topicId
     */
    public function getPosts($topicId){
        $select = $this->select()->setIntegrityCheck(false);// 可以跨表查询
        $select->from('forum_posts', '*');
        $select->where('topic_id  = ?', $topicId);
        $select->join('core_users','forum_posts.user_id = core_users.id', array('username', 'avatar', 'role', 'time_reg'));
        
        $adapter = new Zend_Paginator_Adapter_DbTableSelect($select);
        
        return $adapter;
        
    }
    
    /**
     * 获取最后回复的帖子
     *
     * 某一主题的最后回复帖，按时间降序排列
     *
     * @param int $topicId
     */
    public function getLastpost($topicId){
        $select = $this->select();
        $select->where("topic_id = ?", $topicId);
        $select->order('time DESC');
        $row = $this->fetchRow($select);
        if ($row){
            return $row;
        }
        else{
            return null;
        }
    }
    
    /**
     * 计算回复帖数量
     *
     * 某一主题的回复数量，返回值是统计数字
     *
     * @param int $topicId
     */
    public function countPosts($topicId){
        $select = $this->select();
        $select->where('topic_id = ?', $topicId);
        $postNum = count($this->fetchAll($select));
        
        return $postNum;
    }
    
    /**
     * 创建回复帖
     *
     * @param int $topicId
     * @param int $userId
     * @param string $post
     */
    public function createPost($topicId, $userId, $post){
        $row = $this->createRow();
        $row->time = time();
        $row->topic_id = $topicId;
        $row->user_id = $userId;
        $row->post = $post;
        $row->save();
        if ($row){
            return $row->id;
        }
        else{
            return null;
        }
    
    }
    
    /**
     * 更新帖子
     *
     * @param intt $id
     * @param array $values
     */
    public function updatePost($id,$values = array())
    {
        $row = $this->find($id)->current();
        $row->post = $values['post'];
        
        $row->save();
        if($row)
        {
            return true;
        }
        else{
            return false;
        }
    }
    
    /**
     * 删除帖子
     *
     * @param int $id
     * @throws Zend_Exception
     */
    public function deletePost($id)
    {
        $row = $this->find($id)->current();
        if ($row){
            $row->delete();
        }
        else{
            throw new Zend_Exception('删除出错！没有找到该帖。');
        }
    }


}

